package com.project.webpage.controller;


import com.github.pagehelper.PageInfo;
import com.project.webpage.aop.TagLog;
import com.project.webpage.dto.FileParam;
import com.project.webpage.dto.LineAppealedDto;
import com.project.webpage.dto.LineAppealedParam;
import com.project.webpage.dto.SearchByMonthParam;
import com.project.webpage.entity.LineAppealed;
import com.project.webpage.service.LineAppealedService;
import com.project.webpage.util.RestResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/webpage/line/appealed/")
public class LineAppealedController extends BaseController {

    @Resource
    LineAppealedService lineAppealedService;

    @Value("${file.uploadPath}")
    private String uploadPath;

    /**
     * 线路申诉列表查询
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询线路月考评申诉列表")
    public RestResult page(@RequestBody LineAppealedParam param) {
        this.getPage(true, param);
        return RestResult.ok(new PageInfo<>(lineAppealedService.getListForPage(param)));
    }

    /**
     * 线路申诉列表
     */
    @PostMapping("output")
    @TagLog(operation = 5, content = "导出线路月考评申诉列表")
    public RestResult output(@RequestBody LineAppealedParam param) {
        this.getPage(false, param);
        return RestResult.ok(lineAppealedService.getListForPage(param));
    }

    /**
     * 线路申诉审评区县列表
     */
    @PostMapping("district")
    public RestResult getDistrict(@RequestBody SearchByMonthParam param) {
        return RestResult.ok(lineAppealedService.getDistrict(param));
    }

    /**
     * 线路月考评申诉附件上传
     */
    @PutMapping(value = "file/upload")
    @TagLog(operation = 2, content = "线路月考评申诉附件上传")
    public RestResult upload(FileParam param) {
        try {
            if (param.getFile() == null || param.getFile().isEmpty()) {
                return RestResult.error("上传文件不能为空");
            }
            File fileFold = new File(uploadPath);
            if (!fileFold.isDirectory()) {
                fileFold.mkdirs();
            }
            String fileName = param.getFile().getOriginalFilename();
            String fileType = "";
            if (fileName.lastIndexOf(".") > 0) {
                fileType = "." + fileName.substring(fileName.lastIndexOf(".") + 1);
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String uploadFileName = df.format(new Date()) + fileType;
            param.getFile().transferTo(new File(fileFold, uploadFileName));
            LineAppealedDto dto = new LineAppealedDto();
            dto.setPdf("/api/webpage/file/" + uploadFileName);
            return RestResult.ok(dto);
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 新增线路月考评申诉
     */
    @PutMapping("add")
    @TagLog(operation = 2, content = "新增线路月考评申诉")
    public RestResult add(@RequestHeader(value = "user") String user, @RequestBody LineAppealed param) {
        try {
            param.setLogonname(user);
            param.setUpdatedt(new Date());
            param.setAppealresult(2);
            lineAppealedService.insert(param);
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 更新线路月考评申诉
     */
    @PutMapping("update")
    @TagLog(operation = 3, content = "更新线路月考评申诉")
    public RestResult update(@RequestBody LineAppealed param) {
        List<LineAppealed> list = lineAppealedService.getById(param.getId());
        if (list.size() == 0) {
            return RestResult.error("该条数据不存在");
        } else {
            try {
                if (param.getAppealresult() == 1 && list.get(0).getDistrictid() != 1) {
                    param.setAppealresult(2);
                    param.setDistrictid(1);
                }
                param.setUpdatedt(new Date());
                param.setId(null);
                lineAppealedService.update(param, list.get(0).getId());
                return RestResult.ok();
            } catch (Exception e) {
                return RestResult.error(e.getMessage());
            }
        }
    }
}
